package com.yunshan.job;

import com.yunshan.mp.service.IMpIndexActivityNoticeService;
import com.yunshan.mp.service.IMpIndexBannerService;
import com.yunshan.mp.service.IMpIndexPreviewService;
import com.yunshan.welfare.service.IWelfareCouponRecordService;
import com.yunshan.welfare.service.IWelfareGiftRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.yunshan.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * 定时任务
 * @author admin001
 */
@Component
public class TaskScheduler {

    @Autowired
    private IMpIndexBannerService mpIndexBannerService;
    @Autowired
    private IMpIndexActivityNoticeService mpIndexActivityNoticeService;
    @Autowired
    private IMpIndexPreviewService mpIndexPreviewService;
    @Autowired
    private IWelfareCouponRecordService welfareCouponRecordService;
    @Autowired
    private IWelfareGiftRecordService welfareGiftRecordService;

    /**
     * 首页banner和公告上下架
     */
    @Scheduled(cron = "0 */15 * * * ?")
    public void executeDailyTask() {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String formattedDate = dateFormat.format(date);
            log.info("定时任务执行时间: {}", formattedDate);

            //预览页
            int previewOn = mpIndexPreviewService.updateMpIndexPreviewStatus(0);
            log.info("预览页上架数量: {}", previewOn);
            int previewOff = mpIndexPreviewService.updateMpIndexPreviewStatus(1);
            log.info("预览页下架数量: {}", previewOff);

            //banner上下架
            int bannerOn = mpIndexBannerService.updateMpIndexBannerStatus(0);
            log.info("banner上架数量: {}", bannerOn);
            int bannerOff = mpIndexBannerService.updateMpIndexBannerStatus(1);
            log.info("banner下架数量: {}", bannerOff);

            //活动公告上下架
            int activityOn = mpIndexActivityNoticeService.updateMpIndexActivityStatus(0);
            log.info("活动公告上架数量: {}", activityOn);
            int activityOff = mpIndexActivityNoticeService.updateMpIndexActivityStatus(1);
            log.info("活动公告下架数量: {}", activityOff);


        }catch (Exception e){
            log.error("定时任务执行失败：{}",e.getMessage());
        }
    }

    /**
     * 礼包和优惠券过期
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void executeGiftTask() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String formattedDate = dateFormat.format(date);
        log.info("定时任务执行时间: {}", formattedDate);

        //礼包失效
        int i = welfareGiftRecordService.updateWelfareGiftRecordStatus();
        log.info("礼包失效数量: {}", i);

        //优惠券失效
        int j = welfareCouponRecordService.updateWelfareCouponRecordStatus();
        log.info("优惠券失效数量: {}", j);
    }
}
